/*
 * usb_camera.h
 *
 *  Created on: Feb 27, 2014
 *      Author: Jim Carroll
 */

#include "usb_camera.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/usbdi.h>
#include <signal.h>
#include <errno.h>

#define DEVICE_MEM 4096

void insertion(struct usbd_connection *usbd_conn,
		usbd_device_instance_t *instance)
{
	printf("USB device connected\n");

	struct usbd_device *device;
	int err;

	err = usbd_attach(usbd_conn, instance, 0, &device);

	// check err


	EIO;
	uint8_t buf[BUF_SIZ];
	err = usbd_descriptor(device, 0, USB_DESC_DEVICE, USB_RECIPIENT_DEVICE, 0,
			0x0409, buf, BUF_SIZ);


	if (err != EOK) {
		return;
	}

	printf("Device class: %x\n", buf[4]);
}

void removal(struct usbd_connection *usbd_conn,
		usbd_device_instance_t *instance)
{
	printf("USB device removed\n");
}

void event(struct usbd_connection *usbd_conn, usbd_device_instance_t *instance,
		uint16_t type)
{
	printf("USB event\n");
}

int main(int argc, char *argv[])
{
	printf("Welcome to the QNX Momentics IDE\n");

	usbd_device_ident_t interest =
	{ USBD_CONNECT_WILDCARD, USBD_CONNECT_WILDCARD, USBD_CONNECT_WILDCARD,
			USBD_CONNECT_WILDCARD, USBD_CONNECT_WILDCARD, };
	usbd_funcs_t funcs =
	{ _USBDI_NFUNCS, insertion, removal, event };
	usbd_connect_parm_t cparms =
	{ NULL, USB_VERSION, USBD_VERSION, 0, argc, argv, 0, &interest, &funcs };

	struct usbd_connection *connection;
	int error;

	error = usbd_connect(&cparms, &connection);

	printf("%d\n", error);

	sigset_t set;
	sigfillset(&set);

	sigwaitinfo(&set, NULL);

	return EXIT_SUCCESS;
}
